package com.jchy.renderit.displayable;
 
import com.jchy.renderit.core.Displayable;
import com.jchy.renderit.core.Hit;
import com.jchy.renderit.core.Point;

public class Lens extends Displayable
{
    
    private Sphere aSphere;
    private Sphere bSphere;

    public Lens()
    {
        super();

        aSphere = new Sphere();
        bSphere = new Sphere();

        aSphere.translate(0.5,0.0,0.0);
        bSphere.translate(-0.5,0.0,0.0);
        
    }

    public Hit intersect(Point prp, Point vrp)
    {
        Hit result;

        Point utprp=untransform(prp);
        Point utvrp=untransform(vrp);

		Hit aHit;
		Hit bHit;

        aHit=aSphere.intersect(utprp,utvrp);
        bHit=bSphere.intersect(utprp,utvrp);

        if(aHit.isHit && bSphere.isInside( aHit.location ) 
        && bHit.isHit && aSphere.isInside( bHit.location ) 
       	&& aHit.location.Dist(prp) < bHit.location.Dist(prp))
       		result=aHit;
       	else if(aHit.isHit && bSphere.isInside( aHit.location ) 
        && bHit.isHit && aSphere.isInside( bHit.location ) 
       	&& aHit.location.Dist(prp) > bHit.location.Dist(prp))
			result=bHit;
       	else if(aHit.isHit && bSphere.isInside( aHit.location ) )
       		result=aHit;
       	else if(bHit.isHit && aSphere.isInside( bHit.location ) )
			result=bHit;
		else 
			result=new Hit();
        
        if(result.isHit)
        {
            result.location = untransform(result.location);
            result.normpt = untransform(result.normpt);
            result.color = new java.awt.Color(getPrimaryColorRed(),getPrimaryColorGreen(),getPrimaryColorBlue());
            result.ambient=GetAmbient();
            result.diffuse=GetDiffuse(); 
            result.phongCoef=GetPhongCoef();
            result.phongExp=GetPhongExp();
        }
        
        return result;

    }
}
    